package com.diandiansong.app.ui.user;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.diandiansong.app.R;
import com.diandiansong.app.entity.MyInfo;
import com.diandiansong.app.entity.UploadInfo;
import com.diandiansong.app.ui.base.BaseAct;
import com.diandiansong.app.ui.cart.AddressChoose;
import com.diandiansong.app.utils.A;
import com.diandiansong.app.utils.Api;
import com.yanzhenjie.album.Album;

import java.io.File;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import diandiansong.library.utils.BaseEntity;
import diandiansong.library.utils.CCallBack;
import diandiansong.library.utils.ImgLoader;
import diandiansong.library.utils.UIHelper;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;

/**
 * 个人资料
 */
public class UserInfo extends BaseAct {
    @BindView(R.id.iv_avatar)
    ImageView mIvAvatar;
    @BindView(R.id.tv_company_name)
    TextView mTvCompanyName;
    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.tv_phone)
    TextView mTvPhone;

    static MyInfo mMyInfo;

    public static void start(Context context, MyInfo myInfo) {
        mMyInfo = myInfo;
        Intent starter = new Intent(context, UserInfo.class);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitle("个人资料");

        addContentView(R.layout.user_info);

        setText(mTvCompanyName, mMyInfo.getData().getCompany());
        setText(mTvName, mMyInfo.getData().getRealname());
        setText(mTvPhone, mMyInfo.getData().getMobile());
        ImgLoader.displayRound(mIvAvatar, mMyInfo.getData().getAvatar());
    }

    @OnClick({R.id.go_upload_avatar, /*R.id.go_edit_company_name, R.id.go_edit_name,*/ R.id.tv_go_address})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.go_upload_avatar:
                Album.album(this)
                        .requestCode(999) // Request code.
                        //.toolBarColor(toolbarColor) // Toolbar color.
                        //.statusBarColor(statusBarColor) // StatusBar color.
                        //.navigationBarColor(navigationBarColor) // NavigationBar color.
                        .title("选择图片") // Title.
                        .selectCount(1) // Choose up to a few pictures.
                        .columnCount(2) // Number of albums.
                        .camera(true) // Have a camera function.
                        //.checkedList(mImageList) // Has selected the picture, automatically select.
                        .start();
                break;
            case R.id.go_edit_company_name:
                break;
            case R.id.go_edit_name:
                break;
            case R.id.tv_go_address:
                AddressChoose.start(getActivity());
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 999) {
            if (resultCode == RESULT_OK) { // Successfully.
                // Parse select result.
                final ArrayList<String> imageList = Album.parseResult(data);
                if (imageList.size() > 0) {
                    onLoading();

                    //压缩图片
                    Luban.with(this)
                            .load(new File(imageList.get(0)))                     //传人要压缩的图片
                            .setCompressListener(new OnCompressListener() { //设置回调
                                @Override
                                public void onStart() {
                                    // TODO 压缩开始前调用，可以在方法内启动 loading UI
                                }

                                @Override
                                public void onSuccess(File file) {

                                    Api.upload(file, new CCallBack<UploadInfo>() {
                                        @Override
                                        public void onSuccess(UploadInfo uploadInfo) throws Exception {
                                            final String url = uploadInfo.getData().getUrl();
                                            A.myHead(url, null, new CCallBack<BaseEntity>() {
                                                @Override
                                                public void onSuccess(BaseEntity baseEntity) throws Exception {
                                                    onLoaded();
                                                    ImgLoader.displayRound(mIvAvatar, "file://" + imageList.get(0));
                                                    UserMain.doRefresh();
                                                    toast("更新头像成功");
                                                }

                                                @Override
                                                public void onFailure(int code, String msg) {
                                                    onLoaded();
                                                    toast(msg);
                                                }
                                            });
                                        }

                                        @Override
                                        public void onFailure(int code, String msg) {
                                            toast(msg);
                                        }
                                    });
                                }

                                @Override
                                public void onError(Throwable e) {
                                    // TODO 当压缩过程出现问题时调用
                                    onLoaded();
                                    UIHelper.toast(getActivity(), "压缩过程出现问题");
                                }
                            }).launch();    //启动压缩
                }

            } else if (resultCode == RESULT_CANCELED) {
                // User canceled.
            }
        }
    }

}
